package com.example.rxjavamodule.simple;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.rxjavamodule.R;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = "XIN";
    public static final String IMAGE_URL = "http://pic1.win4000.com/wallpaper/c/53cdd1f7c1f21.jpg";
    private ImageView imageView;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.image);
    }

    public void onClickAction(View view) {
        /**
         * TODO RX思维
         *
         * 起点 和 终点
         * 起点：被观察者  Observable
         * 终点：观察者    Observer
         */
        Observable.just(IMAGE_URL)
                // 需求：001 图片下载需求
                .map(new Function<String, Bitmap>() {
                    @Override
                    public Bitmap apply(String s) throws Exception {
                        URL url = new URL(s);
                        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                        httpURLConnection.setConnectTimeout(5000);
                        int responseCode = httpURLConnection.getResponseCode();
                        if(responseCode == HttpURLConnection.HTTP_OK){
                            InputStream inputStream = httpURLConnection.getInputStream();
                            return BitmapFactory.decodeStream(inputStream);
                        }
                        return null;
                    }
                })
                .map(new Function<Bitmap, Bitmap>(){

                    @NonNull
                    @Override
                    public Bitmap apply(@NonNull Bitmap bitmap) throws Exception {
                        Paint paint = new Paint();
                        paint.setAntiAlias(true);
                        paint.setColor(Color.RED);
                        paint.setTextSize(88);
                        Bitmap bitmap1 = drawTextToBitmap(bitmap, "这是水印", paint,88, 88);
                        return bitmap1;
                    }
                })
                // 给上面的分配异步线程（图片下载操作）
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                //关联：观察者设计模式   关联起点和终点 == 订阅
                .subscribe(new Observer<Bitmap>() {
                    // 订阅成功
                    @Override
                    public void onSubscribe(Disposable d) {
                        //正在加载中
                        progressDialog = new ProgressDialog(MainActivity.this);
                        progressDialog.setTitle("图片请求中……");
                        progressDialog.show();
                    }
                    // 上一层给我的响应
                    @Override
                    public void onNext(Bitmap value) {
                        imageView.setImageBitmap(value);
                    }
                    // 链条发生了异常
                    @Override
                    public void onError(Throwable e) {

                    }
                    // 整个链条全部结束
                    @Override
                    public void onComplete() {
                        progressDialog.hide();
                    }
                });


    }

    private Bitmap drawTextToBitmap(Bitmap bitmap, String text, Paint paint, int paddingLeft, int paddingTop) {
        Bitmap.Config bitmapConfig = bitmap.getConfig();
        paint.setDither(true);
        paint.setFilterBitmap(true);
        if(bitmapConfig == null){
            bitmapConfig = Bitmap.Config.ARGB_8888;
        }
        bitmap =  bitmap.copy(bitmapConfig, true);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawText(text, paddingLeft, paddingTop, paint);
        return bitmap;
    }
}